package edu.pku.sei.modeler.uml.commands.interactiveoverview;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.uml.BasicActivities.ActivityEdge;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Interaction;
import edu.pku.sei.modeler.model.uml.FundamentalActivities.ActivityNode;
import edu.pku.sei.modeler.uml.commands.ConnectionModelCreateCommand;

public class InteractionOverviewControlFlowCreateCommand extends
		ConnectionModelCreateCommand {
	
	public InteractionOverviewControlFlowCreateCommand(
			MetaModelElement connectionModel, MetaModelElement sourceModel) {
		super(connectionModel, sourceModel);
	}
	@Override
	public boolean canExecute() {
		return super.canExecute();
	}
	
	@Override
	public void execute() {
		if (this.sourceModel instanceof Interaction) {
			((Interaction)this.sourceModel).getOutgoings().add(this.connectionModel);
		} else if (this.sourceModel instanceof ActivityNode) {
			((ActivityNode)this.sourceModel).getOutgoing().add(this.connectionModel);
		}
		
		if (this.targetModel instanceof Interaction) {
			((Interaction)this.targetModel).getIncomings().add(this.connectionModel);
		} else if (this.targetModel instanceof ActivityNode) {
			((ActivityNode)this.targetModel).getIncoming().add(this.connectionModel);
		}
		
		connectionModel.setContainer(sourceModel);
	}
	
	@Override
	public void undo() {
		if (this.sourceModel instanceof Interaction) {
			((Interaction)this.sourceModel).getOutgoings().remove(this.connectionModel);
		} else if (this.sourceModel instanceof ActivityNode) {
			((ActivityNode)this.sourceModel).getOutgoing().remove(this.connectionModel);
		}
		
		if (this.targetModel instanceof Interaction) {
			((Interaction)this.targetModel).getIncomings().remove(this.connectionModel);
		} else if (this.targetModel instanceof ActivityNode) {
			((ActivityNode)this.targetModel).getIncoming().remove(this.connectionModel);
		}
	}
}
